// 1.导入
import { createRouter, createWebHashHistory } from 'vue-router'
import { getStorage } from '@/utils/storage'
import { useStore } from '@/store'
const routes = [
  {
    path: '/Login', name: '登录', component: () => import('@/views/Login/index.vue'),
  },
  {
    path: '/', name: '布局', component: () => import('@/components/layout/index.vue'),
    redirect:"/Home",
    children: [
      {
        path: '/Home', name: '首页', component: () => import('@/views/Home/index.vue'),
      },
      {
        path: '/Hero', component: () => import('@/views/Hero/index.vue'),
        meta: {
          name: '/Hero',
          title: '英雄列表'
        }
      },
      {
        path: '/RankDouble', component: () => import('@/views/RankDouble/index.vue'),
        meta: {
          name: '/RankDouble',
          title: '双排胜率'
        }
      },
    ]

  },
  { path: '/Child', name: '', component: () => import('../components/Child.vue') },
  { path: '/:pathMatch(.*)', component: () => import('../components/404.vue') }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes, // short for `routes: routes
})

router.beforeEach((to, from, next) => {
  const store = useStore()
  if (to.path === '/Login') next()
  if (!getStorage('username', 24 * 60 * 60 * 1000))
    next({ path: '/Login' })
  else
    next()
  if(JSON.stringify(to.meta) =="{}")return;
  const name: any = to.meta?.name;
  const title: any = to.meta?.title;
  if (store.tabList.findIndex(item => item.name == name) === -1) {
    store.tabList.push({
      name,
      title
    })
  }
  store.tabsValue = name;
})


// 导出路由
export default router
